import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/closure/access_record_entity.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class AccessRecordController extends GetxController {


  // 输入的人员名称
  String? searchPersonName;

  // 输入的车牌号
  String? searchCarName;

  // 选中的人员类型
  String? searchPersonType;
  // 选中的人员类型显示
  String? searchPersonTypeShow;

  // 选中的车辆类型
  String? searchCarType;
  // 选中的车辆类型显示
  String? searchCarTypeShow;

  // 开始时间
  String? searchStartTime;
  // 结束时间
  String? searchEndTime;

  /// 搜索使用key
  final GlobalKey<CustomEasyRefreshListState> globalKeyCar = GlobalKey();
  final GlobalKey<CustomEasyRefreshListState> globalKeyPerson = GlobalKey();

  /// 筛选右侧栏控制Key
  final popRightScaffoldKeyCar = GlobalKey<ScaffoldState>();
  final popRightScaffoldKeyPerson = GlobalKey<ScaffoldState>();

  /// 筛选
  void screen(int i) {
    if (i == 1) {
      popRightScaffoldKeyPerson.currentState?.openEndDrawer();
    } else {
      popRightScaffoldKeyCar.currentState?.openEndDrawer();
    }
  }

  // 跳转详情 1: 人员 2: 车辆
  void toDetails(AccessRecordEntity item,int i) {
    Get.toNamed(AppRoutes.accessRecordDetails,arguments: {'item': item,'type': i});
  }

  /// 搜索
  void refreshPage(int i) {
    if(i == 1) {
      globalKeyPerson.currentState?.onRefresh();
    }else {
      globalKeyCar.currentState?.onRefresh();
    }
  }

  // 查询查询出入记录
  Future<List<AccessRecordEntity>> checkAssetsDetails(int pageNum,int pageSize, int type, {String? search}) async {
    List<AccessRecordEntity> list = [];
    final res = await HttpUtil().generateGetRequest<List<AccessRecordEntity>>(Interface.getRecordPage,queryParameters: {
      'pageNum': pageNum,
      'pageSize': pageSize,
      'carNumber': type == 0 ? searchCarName : null,// 车牌号
      'name': type == 1 ? searchPersonName : null,// 人员名称
      'type': type , // 0:车辆 1:人员
      'startDateTime': searchStartTime != null ? '$searchStartTime 00:00:00' : null,
      'endDateTime': searchEndTime != null ? '$searchEndTime 00:00:00' : null,
      'carDictId': type == 0 ? searchCarType : null,
      'personnelCategory': type == 1 ? searchPersonType : null,
    }).execute();

    res.onSuccess((result) {
      if(result.data != null) {
        list = result.data!;
      }
    });
    return list;
  }

  // 重置
  void reset() {
    searchPersonName = null;
    searchCarName = null;
    searchPersonType = null;
    searchPersonTypeShow = null;
    searchStartTime = null;
    searchEndTime = null;
    searchCarType = null;
    searchCarTypeShow = null;
    update();
  }

  // 确定
  void confirm(int i) {
    refreshPage(i);
    Get.back();
  }

  @override
  void onClose() {
    globalKeyCar.currentState?.dispose();
    globalKeyPerson.currentState?.dispose();
    popRightScaffoldKeyCar.currentState?.dispose();
    popRightScaffoldKeyPerson.currentState?.dispose();
    super.onClose();
  }


}
